package org.langke.design.pattern.strategy;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付方式工厂类
 */
public class PaymentFactory {
    private static Map<String,PaymentStrategy> register = new HashMap<>();
    static {
        for(PaymentEnum p : PaymentEnum.values()){
            register.put(p.getPayName(),p.getPaymentStrategy());
        }
    }

    /**
     * 单例+饿汉(注册表)
     *
     */
    public static PaymentStrategy getInstance(String payType){
        if (register.containsKey(payType)){
            return register.get(payType);
        }
        return PaymentEnum.DEFAULT.getPaymentStrategy();
    }
}
